﻿#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
#include <string>
#include <map>
#include <vector>
using namespace std;

int main2()
{
	//int arr[6] = { 8,6,2,5,4,3};
	//set<int> s;
	//for (auto t : arr)
	//{
	//	s.insert(t);
	//}
	//set<int>::iterator il = s.begin();
	//
	//while (il != s.end())
	//{
	//	cout << *il << " ";
	//	il++;
	//}

	set<string> s({ "hello", "kivotos", "bit" });
	auto i = s.begin();
	while (i != s.end())
	{
		cout << *i << " ";
		i++;
	}

	return 0;
}

int main3()
{
	map<string, string> m;
	pair<map<string,string>::iterator,bool> ret = m.insert({ "left","左"});
	map<string, string>::iterator it = ret.first;
	it->second = "zuo";
	return 0;
}

int main5()
{
	string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉" };
	map<string, int> CountMap;
	for (const auto& fruit : arr)
	{
		//auto ret = CountMap.find(fruit);
		//if (ret == CountMap.end())
		//{
		//	CountMap.insert({ fruit,1 });
		//}
		//else
		//{
		//	ret->second++;
		//}
		CountMap[fruit]++;//没找到，则插入，并返回插入位置对应的迭代器，通过迭代器访问其数据并修改，默认是0，++变为1
					      //找到了，不插入，返回相等数据位置对应的迭代器，通过迭代器访问其数据并修改，原来的数++；
	}
	for (const auto& m : CountMap)
	{
		cout << m.first << ":" << m.second << endl;
	}
	return 0;
}

//两个数组的交集
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
	set<int> s1(nums1.begin(), nums1.end());
	set<int> s2(nums2.begin(), nums2.end());
	vector<int> tmp;

	set<int>::iterator it1 = s1.begin();
	set<int>::iterator it2 = s2.begin();
	while (it1 != s1.end() && it2 != s2.end())
	{
		if (*it1 < *it2)
		{
			it1++;
		}
		else if (*it1 > *it2)
		{
			it2++;
		}
		else
		{
			tmp.push_back(*it1);
			it1++;
			it2++;
		}
	}
	return tmp;
}

int main9()
{
	vector<int> v1({ 1,2,2,1 });
	vector<int> v2({ 2,2});
	intersection(v1, v2);
	return 0;
}

int main()
{
	map<string, string> dirt;
	dirt.insert({ "kayoko","3-17" });
	dirt.insert({ "hina","2-19" });
	dirt.insert({ "kisaki","2-19" });
	auto it = dirt.begin();
	while (it != dirt.end())
	{
		dirt["jc"];
		it++;
	}

	auto result = dirt.find("jc");
	result->second = "10-13";

	for (const auto& i : dirt)
	{
		cout << i.first << ":" << i.second << endl;
	}

	return 0;
}

//stl的综合运用
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

struct Com
{
	bool operator()(const pair<string, int>& x1, const pair<string, int>& x2)
	{
		return x1.second > x2.second;
	}
};

int main() {
	//输入一个英文句子
	string sentence;
	getline(cin, sentence);


	//将字母一一存到vector当中
	vector<string> v;
	string tmp;
	for (int i = 0; i < sentence.size(); i++)
	{
		if (sentence[i] >= 'a' && sentence[i] <= 'z')
		{
			tmp.push_back(sentence[i]);
		}
		else if (sentence[i] >= 'A' && sentence[i] <= 'Z') //大小写转换
		{
			sentence[i] += 32;
			tmp.push_back(sentence[i]);
		}
		else
		{
			v.push_back(tmp);
			tmp.clear();//清空tmp，为了下一次能够识别单词
		}
	}
	//统计字母出现的次数
	map<string, int> CountMap;
	for (auto& e : v)
	{
		CountMap[e]++;
	}

	//将map中的数据默认是按key排列的，因此需要重新排列，保存到vector中进行重新排列
	vector<pair<string, int>> v2(CountMap.begin(), CountMap.end());
	stable_sort(v2.begin(), v2.end(), Com());//默认的函数不满足我们的排序，因此需要自己写一个仿函数，并且通过匿名对象调用

	//最后遍历排列好的vector 输出最终答案
	for (int i = 0; i < v2.size(); i++)
	{
		cout << v2[i].first << ":" << v2[i].second << endl;
	}
	return 0;
}